//
//  BindEmailView.swift
//  Exchange
//
//  Created by 孟祥群 on 2018/6/7.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class BindEmailView: BaseView, UITableViewDataSource, UITableViewDelegate, TimerDelegate{
    var viewModel:BindEmailViewModel?
    var timerBtn:TimerBtn?
    var codeTimerBtn:TimerBtn?
    
    var emailText:BaseTextField?
    var codeText:BaseTextField?
    var yzText:BaseTextField?
    
    lazy var tableView:UITableView = UITableView(frame: CGRect.zero, style: UITableViewStyle.plain)
    lazy var selectBtn:UIButton = {
        let btn =  CommonBtn.creatSelectBtnWithTitle(title: kText("确定"), y: 28, view: self, sel:  #selector(selectBtnAction))
        return btn
    }()
    
    override func setUpUI() -> Void {
        viewModel = dataSource as? BindEmailViewModel
        
        self.addSubview(tableView)
        tableView.separatorStyle = UITableViewCellSeparatorStyle.none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.backgroundColor = UIColor.clear
        tableView.snp.makeConstraints { (make) in
            make.left.right.bottom.top.equalToSuperview()
        }
        let footView = UIView(frame: CGRect(x: 0, y: 0, width: kSCREEN_WIDTH, height: 100))
        tableView.tableFooterView = footView
        
        footView.addSubview(selectBtn)
        selectBtn.isEnabled = false
        CommonBtn.createImageWithColor(color: kDisableColor, btn: selectBtn, state: UIControlState.normal)
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = viewModel?.cellfactory.cellIdentiferAtIndexPath(indexPath)
        let item = viewModel?.cellfactory.itemAtIndexPath(indexPath)
        var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier!) as? BaseTableViewCell
        if cell == nil {
            switch item!.type! {
            case .cyNameWrite:
                cell = NameWriteCell(style: .default, reuseIdentifier:cellIdentifier)
                break
            case .cyItemTimer:
                cell = TimerTableCell(style: .default, reuseIdentifier:cellIdentifier)
                break
                
            case .cyItemNumber:
                cell = NumberWriteCell(style: .default, reuseIdentifier:cellIdentifier)
                break
            default : break
            }
            cell?.tag = indexPath.row
            cell?.selectionStyle = .none
        }
        
        switch item!.type! {
        case .cyNameWrite:
            let cell = (cell as! NameWriteCell)
            cell.textField.textDelegate = self
            emailText = cell.textField
            cell.setIteam(item!)
            break
        case .cyItemTimer:
            let cell = (cell as! TimerTableCell)
            cell.numTextField.length = 6
            cell.numTextField.textDelegate = self
            cell.delegate = self
            cell.setIteam(item!)
            if indexPath.row == 1 {
                codeText = cell.numTextField
                timerBtn = cell.timerBtn
            }else if indexPath.row == 2{
                yzText  = cell.numTextField
                codeTimerBtn = cell.timerBtn
            }
            
            break
            
        case .cyItemNumber:
            let cell = (cell as! NumberWriteCell)
            cell.numTextField.length = 6
            cell.numTextField.textDelegate = self
            yzText = cell.numTextField
            cell.setIteam(item!)
            break
            
        default : break
        }
        
        
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view:UIView = UIView(frame: CGRect(x: 0, y: 0, width: kSCREEN_WIDTH, height: 10))
        return view
    }
    
    @objc func selectBtnAction() {
        self.endEditing(true)
        if !(viewModel?.emailText.judgeEmail())! {
            CYMBShow.showMessage(kText("请输入正确格式的邮箱"))
            return
        }
        if (viewModel?.codeText.ch_length)! < 6 {
            CYMBShow.showMessage(kText("请输入6位数验证码"))
            return
        }
        if (viewModel?.googleText.ch_length)! < 6 {
            CYMBShow.showMessage(kText("请输入6位数验证码"))
            return
        }
        
        self.viewModel?.sendInformation()
    }
    
    func startTimer(tag: Int, btn: TimerBtn) {
        self.endEditing(true)
        if tag == 1 {
            if !(viewModel?.emailText.judgeEmail())! {
                CYMBShow.showMessage(kText("请输入正确格式的邮箱"))
                return
            }
            self.viewModel?.sendEmailCode(btn: btn)
        }
        if tag == 2{
            self.viewModel?.sendCode(btn: btn)
        }
    }
    
    func changeStates()  {
        var result = true
        if  (viewModel?.model?.isOpenGoogle)! {
            if viewModel?.googleText.ch_length == 0 {
                result = false
            }
        }
        
        if viewModel?.emailText.ch_length != 0 && viewModel?.codeText.ch_length != 0
            && result  {
            selectBtn.isEnabled = true
            CommonBtn.createImageWithColor(color:kThemeColor, btn: selectBtn, state: UIControlState.normal)
        }else {
            selectBtn.isEnabled = false
            CommonBtn.createImageWithColor(color: kDisableColor, btn: selectBtn, state: UIControlState.normal)
        }
        
    }
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
extension BindEmailView:BaseTextFieldDelegate {
    
    func textFieldDidChange(textField: BaseTextField) {
        if textField == emailText {
            viewModel?.emailText = textField.text!
        }else if textField == codeText {
            viewModel?.codeText = textField.text!
        }else {
            viewModel?.googleText = textField.text!
        }
        self.changeStates()
    }
    
}
